package com.hexu.event.dao;

import com.hexu.event.entity.Comment;
import com.hexu.event.entity.User;
import com.hexu.event.util.Base64Util;
import com.hexu.event.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CommentDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    //保存评论
    public boolean addComment(Comment comment){
        String sql = "insert into comment(parent_id,article_name,account,content,comment_time,comment_status)"
                + " values (?,?,?,?,?,?);";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);

            //View层set的数据
            ps.setInt(1, comment.getParentId());
            ps.setString(2, comment.getArticleName());
            ps.setString(3,comment.getAccount());
            ps.setString(4, comment.getContent());
            ps.setString(5,comment.getCommentTime());
            ps.setInt(6, comment.getCommentStatus());

            ps.executeUpdate();

            //添加成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //添加失败
        return false;
    }

    //返回某文章的所有评论
    public List<Comment> getAllComment(String articleName){
        List<Comment> list = new ArrayList<>();
        String sql = "select * from comment where article_name=? ";

        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,articleName);
            rs = ps.executeQuery();

            while (rs.next()){
                Comment comment = new Comment();
                comment.setId(rs.getInt("id"));
                comment.setArticleName(rs.getString("article_name"));
                comment.setAccount(rs.getString("account"));
                comment.setContent(rs.getString("content"));
                comment.setCommentTime(rs.getString("comment_time"));
                comment.setParentId(rs.getInt("parent_id"));
                comment.setCommentStatus(rs.getInt("comment_status"));
                list.add(comment);

            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //返回某一评论
    public Comment getComment(String articleName,String content,String commentTime){
        String sql = "select * from comment where article_name=? and content=? and comment_time=?";

        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);
            ps.setString(1,articleName);
            ps.setString(2,content);
            ps.setString(3,commentTime);
            rs = ps.executeQuery();

            while (rs.next()){
                Comment comment = new Comment();
                comment.setId(rs.getInt("id"));
                comment.setArticleName(rs.getString("article_name"));
                comment.setAccount(rs.getString("account"));
                comment.setContent(rs.getString("content"));
                comment.setCommentTime(rs.getString("comment_time"));
                comment.setParentId(rs.getInt("parent_id"));
                comment.setCommentStatus(rs.getInt("comment_status"));
                return comment;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(conn, ps, rs);
        }
        return null;
    }

    //管理员删除评论回复
    public boolean deleteComment(String articleName,String content,String commentTime){
        String sql = "update user set content=? where article_name=? and content=? and comment_time=?";
        try {
            conn = JdbcUtil.getConn();
            ps = conn.prepareStatement(sql);

            ps.setString(1, "该评论已被管理员删除");
            ps.setString(2,articleName);
            ps.setString(3,content);
            ps.setString(4,commentTime);

            ps.executeUpdate();

            //删除成功
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            JdbcUtil.close(conn, ps, null);
        }

        //删除失败
        return false;
    }

}
